//
//  LeanCloudLoginViewController.m
//  WeChatQQStaticLibDemo
//
//  Created by mac on 17/2/8.
//  Copyright © 2017年 srulos. All rights reserved.
//

#import "LeanCloudLoginViewController.h"
#import "WeChatQQStaticLibHeader.h"
#import "QQLoginViewController.h"
#import "MPLoginViewController.h"
#import "QRCodeLoginViewController.h"
#import "ExplainViewController.h"
#import "LeanCloudRegisterViewController.h"
#import "WeChatQQApi.h"

@interface LeanCloudLoginViewController ()
{
    UIColor * m_mainColor;
    BOOL m_isOpenRegister;
}
@end

@implementation LeanCloudLoginViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.title = [self getLocalizedStr:@"Login"];
    
    
    NSString *cancle = [self getLocalizedStr:@"Cancel"];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:cancle style:UIBarButtonItemStylePlain target:self action:@selector(onDismissCallBack)];
    
    m_isOpenRegister = YES;
    
    CREATE_BORDER_EXTEND(self.m_label_passport_back, 4, 1, 228, 228, 228, 1);
    CREATE_BORDER_EXTEND(self.m_label_password_back, 4, 1, 228, 228, 228, 1);
    
    self.m_label_passport_back.backgroundColor = [UIColor whiteColor];
    self.m_label_password_back.backgroundColor = [UIColor whiteColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textfildChange) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    ADD_OBSERVER(self, @selector(onAppInfoLoadedCB:), NOTIFICATION_APP_INFO_LOADED, nil);
    ADD_OBSERVER(self, @selector(onUserLoginSuccessCB:), NOTIFICATION_USER_LOGIN_SUCCESS, nil);
    
    [self setNavigationColor];
    
    [self showThirdLoginBtn];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setNavigationColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)onLeanCloudRegisterCallBack:(id)sender {
    if (m_isOpenRegister == NO) {
        UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"服务器维护暂时停用注册功能！老用户可正常登录！新用户请使用第三方登录！" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertview show];
    }
    else{
        LeanCloudRegisterViewController *leanCloudRegisterViewController=[[LeanCloudRegisterViewController alloc]initWithNibName:@"LeanCloudRegisterViewController" bundle:nil];
        [self.navigationController pushViewController:leanCloudRegisterViewController animated:true];
    }
}

- (IBAction)onQQLoginCallBack:(id)sender {
    QQLoginViewController * qqLoginViewController = [[QQLoginViewController alloc]initWithNibName:@"QQLoginViewController" bundle:nil];
    [qqLoginViewController updateMainColor:m_mainColor];
    UINavigationController * qqNavigationController = [[UINavigationController alloc]initWithRootViewController:qqLoginViewController];
    [self presentViewController:qqNavigationController animated:YES completion:nil];
}

- (void)setNavigationColor
{
    UINavigationBar *bar = [UINavigationBar appearance];
    bar.barTintColor = m_mainColor;
    bar.tintColor = [UIColor whiteColor];
    [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
}

- (void)updateMainColor:(UIColor *)color
{
    m_mainColor = color;
}

- (void)onDismissCallBack
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)textfildChange{
    
    if (_m_text_passport.text.length > 0 && _m_text_password.text.length > 0) {
        _m_btn_just_login.userInteractionEnabled = YES;
        [_m_btn_just_login setBackgroundColor:HEXCOLOR(0x51C332)];
    }else{
        _m_btn_just_login.userInteractionEnabled = NO;
        _m_btn_just_login.backgroundColor = HEXCOLOR(0xEBEBEB);
    }
}

- (void)showThirdLoginBtn
{
    //如果开启了第三方登录就关闭注册功能
    if([WeChatQQApi stander].m_loginType != LT_NONE){
        m_isOpenRegister = NO;
    }
    
    if([WeChatQQApi stander].m_isLogin == NO){
        if ([WeChatQQApi stander].m_loginType != LT_NONE) {
            _m_label_third_back.hidden = NO;
            _m_label_third_title.hidden = NO;
        }
        
        if([WeChatQQApi stander].m_loginType == LT_QQ){
            _m_btn_qq_login.hidden = NO;
        }
        else if([WeChatQQApi stander].m_loginType == LT_WECHAT || [WeChatQQApi stander].m_loginType == LT_MP || [WeChatQQApi stander].m_loginType == LT_WECHAT_SCAN){
            _m_btn_wechat_login.hidden = NO;
        }
    }
}

#pragma mark app info loaded cb
- (void)onAppInfoLoadedCB:(id)sender
{
    [self showThirdLoginBtn];
}

- (IBAction)onWeChatLoginCallBack:(id)sender {
    if([WeChatQQApi stander].m_isLogin == NO){
        if([WeChatQQApi stander].m_loginType == LT_WECHAT){
            if (isPad) {
                QRCodeLoginIPadViewController * qrCodeLoginViewController = [[QRCodeLoginIPadViewController alloc]initWithNibName:@"QRCodeLoginIPadViewController" bundle:nil];
                qrCodeLoginViewController.m_color = m_mainColor;
                UINavigationController * qrCodeNavigationController = [[UINavigationController alloc]initWithRootViewController:qrCodeLoginViewController];
                [self presentViewController:qrCodeNavigationController animated:YES completion:nil];
            }
            else{
                QRCodeLoginViewController * qrCodeLoginViewController = [[QRCodeLoginViewController alloc]initWithNibName:@"QRCodeLoginViewController" bundle:nil];
                [qrCodeLoginViewController updateSaveBtnColor:m_mainColor];
                UINavigationController * qrCodeNavigationController = [[UINavigationController alloc]initWithRootViewController:qrCodeLoginViewController];
                [self presentViewController:qrCodeNavigationController animated:YES completion:nil];
            }
        }
        else if([WeChatQQApi stander].m_loginType == LT_MP){
            if (isPad) {
                QRCodeLoginIPadViewController * qrCodeLoginViewController = [[QRCodeLoginIPadViewController alloc]initWithNibName:@"QRCodeLoginIPadViewController" bundle:nil];
                qrCodeLoginViewController.m_color = m_mainColor;
                UINavigationController * qrCodeNavigationController = [[UINavigationController alloc]initWithRootViewController:qrCodeLoginViewController];
                [self presentViewController:qrCodeNavigationController animated:YES completion:nil];
            }
            else{
                MPLoginViewController * qrCodeLoginViewController = [[MPLoginViewController alloc]initWithNibName:@"MPLoginViewController" bundle:nil];
                [qrCodeLoginViewController updateSaveBtnColor:m_mainColor];
                UINavigationController * qrCodeNavigationController = [[UINavigationController alloc]initWithRootViewController:qrCodeLoginViewController];
                [self presentViewController:qrCodeNavigationController animated:YES completion:nil];
            }
        }
        else if([WeChatQQApi stander].m_loginType == LT_WECHAT_SCAN){
            QRCodeLoginIPadViewController * qrCodeLoginViewController = [[QRCodeLoginIPadViewController alloc]initWithNibName:@"QRCodeLoginIPadViewController" bundle:nil];
            qrCodeLoginViewController.m_color = m_mainColor;
            UINavigationController * qrCodeNavigationController = [[UINavigationController alloc]initWithRootViewController:qrCodeLoginViewController];
            [self presentViewController:qrCodeNavigationController animated:YES completion:nil];
        }
    }
}

- (IBAction)onShowExplainCallBack:(id)sender {
    ExplainViewController * explainViewController = [[ExplainViewController alloc]initWithNibName:@"ExplainViewController" bundle:nil];
    [self.navigationController pushViewController:explainViewController animated:YES];
}

- (IBAction)onLeanCloudLoginCallBack:(id)sender {
    if (_m_text_passport.text.length >= 6 && _m_text_password.text.length >= 6) {
        
        NSString *username = self.m_text_passport.text;
        NSString *password = self.m_text_password.text;
        
        if (username && password) {
            
            SHOW_HUD([self getLocalizedStr:@"logging in..."]);
            
            
            [[AsyncLeanCloudSimulationService stander]asyncLoginLeanCloud:username password:password respBlock:^(LEANCLOUD_LOGIN_RESULT result) {
                HIDE_HUD;
                
                NSString *hint = [self getLocalizedStr:@"Hint"];
                NSString *msg = [self getLocalizedStr:@"login successful"];
                NSString *confirm = [self getLocalizedStr:@"Confirm"];
                
                if(result == LLR_NOT_INIT){
                    NSString *msg = [self getLocalizedStr:@"The network does not seem to work. Please try again later"];
                    
                    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:hint message:msg delegate:nil cancelButtonTitle:confirm otherButtonTitles:nil, nil];
                    [alertview show];
                }
                else if (result == LLR_SUCCESS) {
                    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:hint message:msg delegate:nil cancelButtonTitle:confirm otherButtonTitles:nil, nil];
                    [alertview show];
                    
                    [self onDismissCallBack];
                }
                else{
                    
                    NSString *msg = [self getLocalizedStr:@"Username or password is wrong!"];
                    
                    UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:hint message:msg delegate:nil cancelButtonTitle:confirm otherButtonTitles:nil, nil];
                    [alertview show];
                }
            }];            
        }
    }
    else
    {
        NSString *msg = [self getLocalizedStr:@"Please fill in the correct account or password! Account password must be less than 6!"];
        SHOW_HUD_MESSAGE(msg);
    }
    
}

#pragma mark 用户登录成功回调，在这里可以获取用户的唯一ID
-(void)onUserLoginSuccessCB:(id)sender
{
    [self.navigationController dismissViewControllerAnimated:false completion:nil];
}



-(NSString *)getLocalizedStr:(NSString *)str{
    return NSLocalizedStringFromTable(str, @"HHLeanCloud", nil);
}

@end
